<template>
  <section>
    <el-button size="mini" @click="addBtn">新建</el-button>
    <el-dialog :title="currPG?'编辑'+currPG.title:'新建'" :visible.sync="dialogVisible" v-if="dialogVisible">
      <p-g v-if="currPG" :PG="currPG" v-on:cancel="dialogVisible=false"></p-g>
      <el-form v-else label-width="80px">
        <el-form-item label="标题">
          <el-input size="mini" v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="编码">
          <el-input size="mini" v-model="form.code"></el-input>
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="selType" size="mini">
            <el-option :value="type.val" v-for="type in typeList" :key="type.val" :label="type.label"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="体质类型">
          <el-select v-model="form.physicalId" size="mini">
            <el-option :value="physical.id" v-for="physical in physicalList" :key="physical.id" :label="physical.name"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="体质等级">
          <el-select v-model="form.gradeId" size="mini">
            <el-option :value="grade.id" v-for="grade in gradeList" :key="grade.id" :label="grade.name+grade.description"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="注意事项" >
          <TextEditor v-model="form.attention" :editorToolbar="customToolbar"></TextEditor>
        </el-form-item>
        <el-form-item label="描述">
          <el-input size="mini" v-model="form.description"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="mini" @click="subAddBtn" type="primary">确认新建</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-row>
      <el-table :data="physicalAndGradeList" stripe v-loading="loading" v-if="physicalList && gradeList">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-width="90px" class="demo-table-expand">
              <el-form-item label="描述：">
                <span v-html="props.row.description"></span>
              </el-form-item>
              <el-form-item label="注意事项：">
                <span v-html="props.row.attention"></span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column type="index" label="序号"width="80px"></el-table-column>
        <el-table-column prop="title" label="名称" width="150px">
        </el-table-column>
        <el-table-column prop="code" label="编码" >
        </el-table-column>
        <el-table-column prop="type" label="人员类型" >
          <template slot-scope="scope">
            {{typeList.find(t=>t.val === scope.row.type).label}}
          </template>
        </el-table-column>
        <el-table-column label="体质类型" >
          <template slot-scope="scope">
            {{physicalList.find(t=>t.id === scope.row.id.physicalId).name}}
          </template>
        </el-table-column>
        <el-table-column label="体质等级" >
          <template slot-scope="scope">
            {{gradeList.find(t=>t.id === scope.row.id.gradeId).name}}-{{gradeList.find(t=>t.id === scope.row.id.gradeId).description}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" fixed="right">
          <template slot-scope="scope">
            <el-button type="primary" @click='editBtn(scope.row)' size="mini">编辑</el-button>
            <el-button type="danger" @click='del(scope)' size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
  </section>
</template>

<script>
  import {mapGetters, mapActions, mapMutations} from 'vuex'
  import Vue from 'vue'//后续添加属性，需要vue联动
  import errHandler from '@/assets/errHandler'
  import TextEditor from '@/components/TextEditor'
  import PG from './editPG'
  export default {
    name: "pAndG",
    components:{TextEditor,PG},
    data() {
      return {
        customToolbar: [['bold','italic','underline'],[{'list':'ordered'},{'list':'bullet'}],['image','code-block']],
        selType:null,
        loading:false,
        dialogVisible:false,
        typeList:[
          {label:'儿童',val:1},
          {label:'成人',val:2},
        ],
        form:{
          title:'',physicalId:'',gradeId:''
        },
        physicalAndGradeList:null,
        gradeList:null,
        physicalList:null,
        currPG:null
      }
    },
    created(){
      this.getPhysicalAndGradeList()
      this.getGradeList()
      this.getPhysicalList()
    },
    computed:{
    },
    watch: {
      selType(nv,ov){
        if(nv){
          this.getGradeList(nv)
          this.getPhysicalList(nv)
        }
      }
    },
    methods: {
      ...mapActions({
        fetch: 'fetch'
      }),
      subAddBtn(){
        this.createPhysicalAndGrade({...this.form,type:this.selType})
      },
      createPhysicalAndGrade(form){
        console.log(form)
        this.fetch({name:"createPhysicalAndGrade",data:form})
          .then(res=>{
            this.physicalAndGradeList.push(res)
            this.dialogVisible=false
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      addBtn(){
        // this.physicalAndGradeList.push({edit:true})
        this.dialogVisible=true
      },
      getPhysicalAndGradeList(){
        this.fetch({name:"getPhysicalAndGradeList"})
          .then(res=>{
            console.log(res)
            this.physicalAndGradeList=res
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      getGradeList(type){
        this.fetch({name:"getGradeList",data:{type:type?type:null}})
          .then(res=>{
            console.log(res)
            this.gradeList=res
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      getPhysicalList(type){
        this.fetch({name:"getPhysicalList",data:{type:type?type:null}})
          .then(res=>{
            console.log(res)
            this.physicalList=res
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      editBtn(row){
        console.log(row)
        this.currPG=row
        this.form={...row.id,...row}
        this.dialogVisible=true
        console.log(this.form)
      },
      del(scope){
        this.loading = true
        this.$confirm('确认删除？')
          .then(()=> {
            this.fetch({name: "deletePhysicalAndGrade", data: {...scope.row.id}})
              .then(res => {
                this.loading = false
                this.physicalAndGradeList.splice(scope.$index, 1)
              })
              .catch(err => {
                console.log(err)
                errHandler.handle(this, err)
              })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
            this.loading = false
          })
      }
    }
  }
</script>

<style scoped>

</style>
